package extend.org.compiere.hrm;

import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.logging.Level;

import org.compiere.model.Mhrmattendance;
import org.compiere.model.Mhrmdayschedule;
import org.compiere.model.X_hrm_absence;
import org.compiere.model.X_hrm_attendance;
import org.compiere.util.CLogger;
import org.compiere.util.DB;
import org.compiere.util.Env;

public class PartnerSchedule extends Named {
	
	public static final String dayoffCode = "Вх"; 			// FREE	
	public static final String vacationCode = "ОТ";			// VACATION
	public static final String withoutpayCode = "Б/С";		// WITHOUT PAY
	public static final String diseaseCode = "Б";			// DISEASE
	public static final String businessTripCode = "К";		// BusinessTrip
	public static final String educationTripCode = "У";		// EducationTrip
	public static final String undocumentedCode = "НН";		// Undocumented
	public static final String quickvacationCode = "?";		// Quick vacation
	public static final String maternityleaveCode = "Р";	// Maternity leave
	public static final String truancyCode = "П";			// Truancy
	public static final String studyleaveCode = "УД";		// Study leave
	public static final String holidayCode = "ПР";			// Holiday
	public static final String childCare = "ОЖ";			// Сhild care	
	public static final String movementCode = "ПВ";			// Movement
	public static final String reviewVacationCode = "ОО";	// ReviewFromVacation

	/**	Static Logger	*/
	private static CLogger	s_log	= CLogger.getCLogger (PartnerSchedule.class);
	
	public static ParamSchedule getParam(int BPartner_ID, int Department_ID, Timestamp date){
		
		int workOneDay = 0;
		int absence  = 0;
		String absenceType  = "";
		int attendance  = 0;
		String attendanceType  = "";
		int shiftOneDay  = 0;
		int holiday = 0;
		int ruleId = 0;
		int depID = 0;

		//
		ParamSchedule param = new ParamSchedule();
		
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		
		// Employee data from the card
				String sql_ = "SELECT * FROM HRM_SCHEDULESETTINGS_V \n " +
						       " WHERE AD_CLIENT_ID=" + Env.getAD_Client_ID(Env.getCtx()) + " \n " +
						       " AND C_BPARTNER_ID=" + BPartner_ID + " \n " +
						       " AND DATEFROM =" + DB.TO_DATE(date) ;
				
		try
		{
			pstmt = DB.prepareStatement(sql_,null);
			rs = pstmt.executeQuery();
			if(rs.next()){
				workOneDay = rs.getInt("workschedule_onedaytype_id");
				absence = rs.getInt("absence_id");
				absenceType = rs.getString("absencetype");
				attendance = rs.getInt("attendance_id");
				attendanceType = rs.getString("attendancetype");
				shiftOneDay = rs.getInt("shiftschedule_onedaytype_id");
				holiday = rs.getInt("holidays_id");
				ruleId = rs.getInt("shedulerule_id");
				depID = rs.getInt("hr_department_id");
			}
				
		}
		catch (SQLException e)
		{
			s_log.log(Level.SEVERE, "product", e);
		}
		finally
		{
			DB.close(rs, pstmt);
			rs = null; pstmt = null;
		}	
		
		// shedule rule ID
		param.setRuleId(ruleId);
		
		Mhrmdayschedule daysheduleOriginal = new Mhrmdayschedule(Env.getCtx(), workOneDay, null);
		if(daysheduleOriginal.gethrm_hourquota().intValue() > 0){
			param.setDayOriginal(1);
			param.setHourOriginal(daysheduleOriginal.gethrm_hourquota().intValue());
		}
		
		Mhrmdayschedule dayshedule = null;
		
		// Shift schedule
		if(shiftOneDay !=0)
			dayshedule = new Mhrmdayschedule(Env.getCtx(), shiftOneDay, null);
		else
			dayshedule = daysheduleOriginal; // Work schedule
		
		param.setHour(dayshedule.gethrm_hourquota().intValue());
		
		String code = "";
		if(dayshedule.get_ID() > 0){
			// check Movement
			if(Department_ID == depID){
				if(dayshedule.gethrm_hourquota().intValue() > 0){
					code = Integer.toString(dayshedule.gethrm_hourquota().intValue());
					param.setDay(1);
				}
				else
					code = dayoffCode;
			}else{
				param.setHour(0);
				if(depID != 0)
					code = movementCode;
				else
					code = "-";
			}
		}else
			code = "-";
		
		param.setCode(code);	
		
		
		// Absence
		if(absence != 0){
			
			param.setDay(0);
			param.setHour(0);
			
			if(absenceType.equals(X_hrm_absence.HRM_ABSENCETYPE_Vacation))
				param.setCode(vacationCode);
			else if(absenceType.equals(X_hrm_absence.HRM_ABSENCETYPE_LeaveWithoutSalary))
				param.setCode(withoutpayCode);
			else if(absenceType.equals(X_hrm_absence.HRM_ABSENCETYPE_EducationLeave))
				param.setCode(studyleaveCode);
			else if(absenceType.equals(X_hrm_absence.HRM_ABSENCETYPE_ShorttimeLeave))
				param.setCode(quickvacationCode);
			else if(absenceType.equals(X_hrm_absence.HRM_ABSENCETYPE_NotDoc))
				param.setCode(undocumentedCode);
			else if(absenceType.equals(X_hrm_absence.HRM_ABSENCETYPE_Illness))
				param.setCode(diseaseCode);
			else if(absenceType.equals(X_hrm_absence.HRM_ABSENCETYPE_Dayoff))
				param.setCode(dayoffCode);
			else if(absenceType.equals(X_hrm_absence.HRM_ABSENCETYPE_Absenteeism))
				param.setCode(truancyCode);
			else if(absenceType.equals(X_hrm_absence.HRM_ABSENCETYPE_BusinessTrip)
					|| absenceType.equals(X_hrm_absence.HRM_ABSENCETYPE_ChangeBusinessTrip)){
				if(dayshedule.gethrm_hourquota().intValue() > 0){
					param.setCode(businessTripCode);
					param.setDay(1);
					param.setHour(dayshedule.gethrm_hourquota().intValue());
				}
			}
			else if(absenceType.equals(X_hrm_absence.HRM_ABSENCETYPE_EducationBusinessTrip)){
				if(dayshedule.gethrm_hourquota().intValue() > 0){
					param.setCode(educationTripCode);
					param.setDay(1);
					param.setHour(dayshedule.gethrm_hourquota().intValue());
				}
			}
			else if(absenceType.equals(X_hrm_absence.HRM_ABSENCETYPE_Pregnancy))
				param.setCode(maternityleaveCode);
			else if(absenceType.equals(X_hrm_absence.HRM_ABSENCETYPE_ChildCare))
				param.setCode(childCare);
			else if(absenceType.equals(X_hrm_absence.HRM_ABSENCETYPE_ReviewFromVacation)){
				if(dayshedule.gethrm_hourquota().intValue() > 0){
					param.setCode(reviewVacationCode);
					param.setDay(1);
					param.setHour(dayshedule.gethrm_hourquota().intValue());
				}
			}
		}
		
		// Attendance
		if(attendance !=0){
			
			Mhrmattendance at = new Mhrmattendance(Env.getCtx(), attendance, null);
			// overtime
			if(attendanceType.equals(X_hrm_attendance.HRM_ATTENDANCETYPE_Overtime)){
				param.setDay(1); 
				param.setHour(dayshedule.gethrm_hourquota().intValue());
				param.setHourOvertime(at.gethrm_hourquota().intValue());
				code = Integer.toString(dayshedule.gethrm_hourquota().intValue())+"/"+Integer.toString(at.gethrm_hourquota().intValue());
				param.setCode(code);
			}
			// over the weekend
			else if(attendanceType.equals(X_hrm_attendance.HRM_ATTENDANCETYPE_Weekends_Holidays)){
				param.setDay(1); 
				param.setHour(at.gethrm_hourquota().intValue());
				param.setCode(Integer.toString(at.gethrm_hourquota().intValue()));
			}
		}
				
		// holidays
				if(holiday != 0){
					//
					param.setDayOriginal(0); param.setHourOriginal(0);
					//
					param.setDay(0); param.setHour(0);
					param.setCode(holidayCode);
				}
				
		return param;
	}
	
}
